package com.ljc.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Auth: Ralf Huang
 * Desc: 平台Json处理的统一工具类
 */
@Slf4j
public class JsonUtils {

    /**
     *  静态内部类，实现 SerializeConfig 的单例
     */
    private static class SingletonHandler{
        private static final SerializeConfig INSTANCE = new SerializeConfig();
        static{
            SingletonHandler.INSTANCE.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
        }
    }


    /**
     * 将map对象转化为json string的方法，如果map为null，则返回null
     * @param map
     * @return
     */
    public static String encodeMap(Map map) {
        if(map == null) {
            return null;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(map);
        return jsonObject.toJSONString();
    }

    public static String encodeObject(Object object) {
        if(object == null) {
            return null;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(object);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 驼峰转下划线
     * @param obj
     * @return
     */
    public static String encodeSnackeCase(Object obj){
        try {
            return JSON.toJSONString(obj, SingletonHandler.INSTANCE);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 驼峰转下划线
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T  decodeSnackeCase(String json,Class<T> clazz){
        T t = null;
        try {
            ParserConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
            t = JSON.parseObject(json,clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            ParserConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
        }
        return t;
    }



    public static Object decodeObject(String str, Class clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        Object obj = null;
        log.info(String.format("String value = [%s]", str));
        try {
            obj = objectMapper.readValue(str, clazz);
        } catch (IOException e) {
            log.error("JsonUtils.decodeObject encounter problem!", e);
        }
        return obj;
    }

    public static Object decodeObjectAllowAnyChar(String str, Class clazz){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,true);
        Object obj = null;
        log.info(String.format("String value = [%s]", str));
        try {
            obj = objectMapper.readValue(str, clazz);
        } catch (IOException e) {
            log.error("JsonUtils.decodeObject encounter problem!", e);
        }
        return obj;
    }


    public static String beanToJson(Object object) {
        return JSONObject.toJSONString(object);
    }

    public static <T> List<T> jsonToBeanList(String json, Class<T> clazz) {
        return JSONArray.parseArray(json, clazz);
    }

    public static <T> T jsonToBean(String json, Class<T> clazz) {
        return JSONObject.parseObject(json, clazz);
    }
}
